Imports Microsoft.VisualBasic
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System
Imports System.Data
Imports GTSDAL
Imports GTSDALTableAdapters
Imports GTSSystem.BLL.Location
Imports GTSSystem.BLL.ContactInfo
Imports GTSSystem.BLL.IdGenerator

Namespace GTSSystem.BLL
    <System.ComponentModel.DataObject()> _
    Public Class Carrier

        Private _carrierAdaptor As CarrierTableAdapter = Nothing
        Private _carrierInfoAdaptor As CarrierINFOTableAdapter = Nothing

        Protected ReadOnly Property CarrierAdaptor() As CarrierTableAdapter
            Get
                If _carrierAdaptor Is Nothing Then
                    _carrierAdaptor = New CarrierTableAdapter
                End If
                Return _carrierAdaptor
            End Get
        End Property

        Protected ReadOnly Property CarrierInfoAdaptor() As CarrierInfoTableAdapter
            Get
                If _carrierInfoAdaptor Is Nothing Then
                    _carrierInfoAdaptor = New CarrierINFOTableAdapter
                End If
                Return _carrierInfoAdaptor
            End Get
        End Property

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
        Public Function getCarrier() As CarrierDataTable
            Try
                Return CarrierAdaptor.GetCarrier()
            Catch ex As Exception
                Throw
            End Try
        End Function

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
        Public Function getCarrierByCarrierID(ByVal CarrierID As Integer) As CarrierDataTable
            Try
                Return CarrierAdaptor.GetCarrierByCarrierID(CarrierID)
            Catch ex As Exception
                Throw
            End Try
        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, False)> _
        Public Function getCarrierByClientBranchId(ByVal BranchID As Integer) As CarrierINFODataTable
            Try
                Return CarrierInfoAdaptor.GetCarrierByClientBranchID(BranchID)
            Catch ex As Exception
                Throw
            End Try
        End Function

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
        Public Function getCarrierInfo() As CarrierInfoDataTable
            Try
                Return CarrierInfoAdaptor.GetCarrierInfo()
            Catch ex As Exception
                Throw
            End Try
        End Function

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
        Public Function getCarrierInfoByCarrierID(ByVal CarrierID As Integer) As CarrierInfoDataTable
            Try
                Return CarrierInfoAdaptor.GetCarrierInfoByCarrierID(CarrierID)
            Catch ex As Exception
                Throw
            End Try
        End Function

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Update, True)> _
        Public Sub UpdateCarrier(ByVal CarrierName As String, _
                                    ByVal ContactInfoID As Integer, _
                                    ByVal StatusID As Integer, _
                                    ByVal ContractStartDate As Date, _
                                    ByVal ContractEndDate As Date, _
                                    ByVal LocationID As Integer, _
                                    ByVal ContactName As String, _
                                    ByVal Phone As String, _
                                    ByVal Fax As String, _
                                    ByVal Email As String, _
                                    ByVal StreetAddress As String, _
                                    ByVal City As String, _
                                    ByVal ProvinceState As String, _
                                    ByVal Country As String, _
                                    ByVal PostalCode As String, _
                                    ByVal CarrierAccountNumber As String, _
                                    ByVal CarrierID As Integer)


            Dim LocationAdaptor As New LocationTableAdapter
            Dim ContactInfoAdaptor As New ContactInfoTableAdapter
            Dim user As MembershipUser = Membership.GetUser
            Dim p As ProfileCommon = ProfileCommon.Create(user.UserName)
            Dim BranchID As Integer = p.BranchId

            Try
                LocationAdaptor.Update(LocationID, City, ProvinceState, Country, PostalCode)
                ContactInfoAdaptor.Update(ContactInfoID, LocationID, ContactName, Phone, Fax, Email, StreetAddress)
                CarrierAdaptor.Update(CarrierName, ContactInfoID, StatusID, ContractStartDate, ContractEndDate, CarrierAccountNumber, CarrierID)
            Catch ex As Exception
                Throw
            End Try

        End Sub

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Insert, True)> _
        Public Sub AddCarrier(ByVal CarrierName As String, _
                                ByVal ContractStartDate As Date, _
                                ByVal ContractEndDate As Date, _
                                ByVal ContactName As String, _
                                ByVal Phone As String, _
                                ByVal Fax As String, _
                                ByVal Email As String, _
                                ByVal StreetAddress As String, _
                                ByVal City As String, _
                                ByVal ProvinceState As String, _
                                ByVal Country As String, _
                                ByVal PostalCode As String, _
                                ByVal CarrierAccountNumber As String)

            Dim LocationAdaptor As New LocationTableAdapter
            Dim ContactInfoAdaptor As New ContactInfoTableAdapter
            Dim ContractAdapter As New ContractTableAdapter

            Dim user As MembershipUser = Membership.GetUser
            Dim p As ProfileCommon = ProfileCommon.Create(user.UserName)


            Dim LocationPK As Integer
            Dim ContactInfoPK As Integer
            Dim StatusID As Integer
            Dim CarrierId As Integer
            StatusID = 1
            Try
                'AddLocation
                LocationPK = GetNextId("Location")
                LocationAdaptor.Insert(LocationPK, City, ProvinceState, Country, PostalCode)
                'AddContactInfo
                ContactInfoPK = GetNextId("ContactInfo")
                ContactInfoAdaptor.Insert(ContactInfoPK, LocationPK, ContactName, Phone, Fax, Email, StreetAddress)
                'AddCarrier
                CarrierId = GetNextId("Carrier")
                CarrierAdaptor.Insert(CarrierId, CarrierName, ContactInfoPK, StatusID, ContractStartDate, ContractEndDate, CarrierAccountNumber)
                ContractAdapter.Insert(GetNextId("Contract"), p.ClientId, p.BranchId, CarrierId, StatusID, ContractStartDate, ContractEndDate)
            Catch ex As Exception
                Throw
            End Try
        End Sub
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, False)> _
        Public Function getCarrierIfLoggedIn() As CarrierINFODataTable
            Dim user As MembershipUser = Membership.GetUser
            Dim p As ProfileCommon = ProfileCommon.Create(user.UserName)
            Dim BranchID As Integer = p.BranchId
            Try
                Return CarrierInfoAdaptor.GetCarrierByClientBranchID(BranchID)
            Catch ex As Exception
                Throw New Exception("getCarrierIfLoggedIn")
            End Try
        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
     Public Function getCarrierinformationByAccountnumber(ByVal Accountnumber As String, ByVal BranchID As Integer) As CarrierINFODataTable
            Try
                Return CarrierInfoAdaptor.GetCarriersbyAccountNumber(Accountnumber, BranchID)
            Catch ex As Exception
                Throw
            End Try
        End Function
    End Class
End Namespace

